package br.unb.cic.lp.gol;

public class Memento {
    Statistics statistics = new Statistics();
    private Cell[][] cells;
    private int height;
    private int width;

    public Memento(int height, int width) {
        this.height = height;
        this.width = width;

        cells = new Cell[height][width];

        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                cells[i][j] = new Cell();
                cells[i][j].addCellListener(statistics);
            }
        }
    }

    public void saveState(Memento memento) {
        memento.saveState(cells, statistics);
    }

    public void saveState(Cell[][] cells, Statistics statistics) {
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                if (cells[i][j].isAlive())
                    this.cells[i][j].revive();
                else
                    this.cells[i][j].kill();
            }
        }
        this.statistics.copiaStatistics(statistics);
    }

    public void undo(Cell[][] cells, Statistics statistics) {
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                if (this.cells[i][j].isAlive())
                    cells[i][j].revive();
                else
                    cells[i][j].kill();
            }

        }
        statistics.copiaStatistics(this.statistics);
    }
}
